// Copyright (C) 2024 Kumo inc.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <cstddef>
#include <tuple>
#include <type_traits>
#include <utility>

#include <deneb/cache.h>
#include <deneb/internal/hash.h>
#include <deneb/internal/utility.h>

namespace deneb {

    /// Wraps a function with a "shallow" LRU cache.
    ///
    /// Given a function, this function will return a new function, where
    /// "top-level" calls are cached. With "top-level" or "shallow", we mean
    /// that recursive calls to the same function are not cached, since those
    /// will call the original function symbol, not the wrapped one.
    ///
    /// \tparam CacheType The cache template class to use.
    /// \param original_function The function to wrap.
    /// \param args Any arguments to forward to the cache.
    /// \returns A new function with a shallow LRU cache.
    template<typename Function,
            template<typename...> class CacheType = Cache,
            typename... Args>
    auto wrap(Function original_function, Args &&... args) {
        return [
                original_function,
                cache_args = std::forward_as_tuple(std::forward<Args>(args)...)
        ](auto &&... arguments) mutable {
            using Arguments = std::tuple<std::decay_t<decltype(arguments)>...>;
            using ReturnType = decltype(
            original_function(std::forward<decltype(arguments)>(arguments)...));

            static_assert(!std::is_void<ReturnType>::value,
                          "Return type of wrapped function must not be void");

            static auto cache =
                    internal::construct_from_tuple<CacheType<Arguments, ReturnType>>(
                            cache_args);

            auto key = std::make_tuple(arguments...);
            auto iterator = cache.find(key);

            if (iterator != cache.end()) {
                return iterator->second;
            }

            auto value =
                    original_function(std::forward<decltype(arguments)>(arguments)...);
            cache.emplace(key, value);

            return value;
        };
    }

    /// Wraps a function with a "shallow" LRU timed cache.
    ///
    /// Given a function, this function will return a new function, where
    /// "top-level" calls are cached. With "top-level" or "shallow", we mean
    /// that recursive calls to the same function are not cached, since those
    /// will call the original function symbol, not the wrapped one.
    ///
    /// \param original_function The function to wrap.
    /// \param args Any arguments to forward to the cache.
    /// \returns A new function with a shallow LRU cache.
    template<typename Function, typename Duration, typename... Args>
    auto timed_wrap(Function original_function, Duration duration, Args &&... args) {
        return wrap<Function, TimedCache>(
                original_function, duration, std::forward<Args>(args)...);
    }

}  //  namespace deneb

